package com.anti.modular.bus.busrealestate.controller;

import cn.hutool.core.date.DateUtil;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.annotation.Permission;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.commom.api.Result;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.bus.billwater.param.BillWaterOnParam;
import com.anti.modular.bus.billwater.result.BillWaterExcelResult;
import com.anti.modular.bus.busrealestate.param.BusRealEstateParam;
import com.anti.modular.bus.busrealestate.result.excel.StateExcel;
import com.anti.modular.bus.busrealestate.service.BusRealEstateService;
import com.anti.modular.commonutils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 房产控制器
 *
 * @author anti
 * @date 2021-04-12 19:05:27
 */
@RestController
@RequestMapping("busRealEstate")
public class BusRealEstateController {

    @Resource
    private BusRealEstateService busRealEstateService;

    /**
     * 查询房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @GetMapping("page")
    public Result page(PageParam pageParam, BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.page(pageParam,busRealEstateParam));
    }

    /**
     * 添加房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @Permission
    @PostMapping("add")
    @BusinessLog(title = "房产_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(BusRealEstateParam.add.class) BusRealEstateParam busRealEstateParam) {
            busRealEstateService.add(busRealEstateParam);
        return Result.success("新增成功");
    }

    /**
     * 删除房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @Permission
    @PostMapping("delete")
    @BusinessLog(title = "房产_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(BusRealEstateParam.delete.class) BusRealEstateParam busRealEstateParam) {
        Result delete = busRealEstateService.delete(busRealEstateParam);
        return delete;
    }

    /**
     * 编辑房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @Permission
    @PostMapping("edit")
    @BusinessLog(title = "房产_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(BusRealEstateParam.edit.class) BusRealEstateParam busRealEstateParam) {
            busRealEstateService.edit(busRealEstateParam);
        return Result.success("修改成功");
    }
    /**
     * 查看房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @RequestMapping("detailByType")
    public Result detailByType(BusRealEstateParam busRealEstateParam) {
        return busRealEstateService.detailByType(busRealEstateParam);
    }
    /**
     * 查看房产
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @GetMapping("detail")
    public Result detail(@Validated(BusRealEstateParam.detail.class) BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.detail(busRealEstateParam));
    }

    /**
     * 房产列表
     *
     * @author anti
     * @date 2021-04-12 19:05:27
     */
    @GetMapping("list")
    public Result list(BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.list(busRealEstateParam));
    }


    @GetMapping("fsslist")
    public Result fsslist(BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.fsslist(busRealEstateParam));
    }

    @GetMapping("getStateList")
    public Result getStateList(BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.getStateList(busRealEstateParam));
    }

    @GetMapping("getAllStateList")
    public Result getAllStateList() {
        return Result.data(busRealEstateService.getAllStateList());
    }
    

    @GetMapping("getStateRoomList")
    public Result getStateRoomList(BusRealEstateParam busRealEstateParam) {
        return Result.data(busRealEstateService.getStateRoomList(busRealEstateParam));
    }
    @GetMapping("getStateRoomListByStateId/{id}")
    public Result getStateRoomListByStateId(@PathVariable(name = "id") Long id) {
        return Result.data(busRealEstateService.getStateRoomListByStateId(id));
    }

    @PostMapping("exportExcel")
    @BusinessLog(title = "房產_導出EXCEL", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void exportExcel(HttpServletResponse response){
        List<StateExcel> exportData = busRealEstateService.getExportData();
        String title="房產信息";
        String fileName="房產信息"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        try {
            ExcelUtils.exportExcel(exportData,title,title, StateExcel.class, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
